package com.wlj.security;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wlj.dto.UserMenuDTO;
import com.wlj.dto.UserResourcesDTO;
import com.wlj.exception.CustomException;
import com.wlj.pojo.TbMenu;
import com.wlj.pojo.TbResource;
import com.wlj.pojo.TbUser;
import com.wlj.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class MyUserDetailsService implements UserDetailsService,SocialUserDetailsService {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("表单登录用户名:"+username);
        TbUser user = userService.getOne(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getUsername, username));
        if(user==null){
            throw new CustomException("1001","用户不存在");
        }
        return buildUser(username);
    }

    //社交登录时使用
    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        logger.info("社交登录用户id:"+userId);

        return buildUser(userId);
    }

    private SocialUserDetails buildUser(String userId){
        UserResourcesDTO userAndResources = userService.getUserAndResources(userId);
        List<TbResource> resources = userAndResources.getResources();
        UserMenuDTO userAndMenus = userService.getUserAndMenus(userId);
        List<TbMenu> menus = userAndMenus.getMenus();
        List<String> menuUrls = menus.stream()
                .filter(e -> StringUtils.isNotBlank(e.getMenuUrl()))
                .map(e -> "GET_"+e.getMenuUrl())
                .collect(Collectors.toList());

        List<String> datas = resources.stream().map(e -> e.getMethodType() + "_" + e.getUrl()).collect(Collectors.toList());
        datas.addAll(menuUrls);
        for (String data : datas) {
            logger.info("资源url:"+data);
        }
        return new SocialUser(userAndResources.getUsername(),userAndResources.getPassword(),
                true,true,true,true,
                AuthorityUtils.createAuthorityList(datas.toArray(new String[datas.size()])));
    }

}
